package com.sy.olddriver.service.impl;

import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.UserDao;
import com.sy.olddriver.dao.impl.UserDaoImpl;
import com.sy.olddriver.entity.User;
import com.sy.olddriver.exception.BusinessException;
import com.sy.olddriver.service.UserService;
import com.sy.olddriver.utils.CookieUtils;
import com.sy.olddriver.utils.RedisUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public void register(String name, String pwd, String pwdConfirm, String phone, String inputValidateCode, String correctValidateCode) {
        //TODO:服务端校验
        if (StringUtils.isBlank(name)) {
            throw new BusinessException("用户名不能为空");
        }
        if (userDao.getAllUserNames().contains(name)) {
            throw new BusinessException("用户名不能重复");
        }
        if (StringUtils.isBlank(pwd) || StringUtils.isBlank(pwdConfirm)) {
            throw new BusinessException("密码不能为空");
        }
        if (!pwd.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$")) {
            throw new BusinessException("密码必须是强密码类型");
        }
        if (!StringUtils.equals(pwd, pwdConfirm)) {
            throw new BusinessException("两次密码输入必须一致");
        }
        if (StringUtils.isBlank(phone)) {
            throw new BusinessException("手机号不能为空");
        }
        if (!phone.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$")) {
            throw new BusinessException("手机号格式不合法");
        }
        if (userDao.getAllPhones().contains(phone)) {
            throw new BusinessException("相同的手机号不能重复注册");
        }
        if (!StringUtils.equalsIgnoreCase(inputValidateCode, correctValidateCode)) {
            throw new BusinessException("验证码不正确");
        }
        int r = userDao.addUser(name, DigestUtils.md5Hex(pwd), phone);
        if (r <= 0) {
            throw new BusinessException("新增失败");
        }
    }

    @Override
    public boolean checkNameDuplicate(String name) {
        return userDao.getAllUserNames().contains(name);
    }

    @Override
    public boolean checkPhoneDuplicate(String phone) {
        return userDao.getAllPhones().contains(phone);
    }

    @Override
    public User login(String name, String pwd) {
        if (StringUtils.isBlank(name)) {
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtils.isBlank(pwd)) {
            throw new BusinessException("密码不能为空");
        }
        User user = userDao.getUserByName(name);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        if (!StringUtils.equalsIgnoreCase(user.getPwd(), DigestUtils.md5Hex(pwd))) {
            throw new BusinessException("密码错误");
        }
        user.setPwd(null);
        return user;
    }

    @Override
    public User getLoginUser(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        } else {
            User user = RedisUtils.loadObj(AppConstant.REDIS_USER_KEY_PREFIX + token);
            return user;
        }
    }
}
